package com.dongdongshop.service.impl.seckill_goods;

import com.dongdongshop.mapper.TbSeckillGoodsMapper;
import com.dongdongshop.mapper.TbSeckillOrderMapper;
import com.dongdongshop.pojo.seckill_goods.TbSeckillGoods;
import com.dongdongshop.pojo.seckill_goods.TbSeckillGoodsExample;
import com.dongdongshop.service.seckill_goods.SeckillGoodsService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class SeckillGoodsServiceImpl implements SeckillGoodsService {
    @Autowired
    private TbSeckillGoodsMapper tbSeckillGoodsMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Value("${seckill.key}")
    String key;
    @Value("${seckill.topic}")
    String topic;
    @Override
    public TbSeckillGoods getSeckillItem(Long id) {
        TbSeckillGoods tbSeckillGoods  = (TbSeckillGoods) redisTemplate.opsForHash().get(key, id);
        return tbSeckillGoods;
    }

    @Override
    public void updateStockCount(Long id, String username) {
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        TbSeckillGoods seckillGoods = (TbSeckillGoods) redisTemplate.opsForHash().get(key, id);
        if (null == seckillGoods){
            throw new RuntimeException("商品不存在");
        }
        if (seckillGoods.getEndTime().getTime()-new Date().getTime()<0){
            throw new RuntimeException("秒杀结束了");
        }
        Integer stockCount = Integer.parseInt(redisTemplate.opsForValue().get(id)+"");
        if (stockCount<=0){
            throw new RuntimeException("商品已售罄");
        }
        Long decrement = redisTemplate.opsForValue().decrement(id);
        seckillGoods.setStockCount(decrement.intValue());
        redisTemplate.opsForHash().put(key,id,seckillGoods);
        if (decrement.intValue()==0){
            tbSeckillGoodsMapper.updateByPrimaryKeySelective(seckillGoods);
            redisTemplate.opsForHash().delete(key,id);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("seckillId",seckillGoods.getId());
        map.put("userId",username);
        rocketMQTemplate.convertAndSend(topic,map);
    }

    @Override
    public List<TbSeckillGoods> listSeckillItems() {
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        //从redis中查询所有要秒杀的
        List<TbSeckillGoods> seckillList = redisTemplate.opsForHash().values(key);
        if (seckillList==null||seckillList.size()<=0){

            TbSeckillGoodsExample example = new TbSeckillGoodsExample();
            TbSeckillGoodsExample.Criteria criteria = example.createCriteria();
            criteria.andStatusEqualTo("1");//表示审核通过
            criteria.andStartTimeLessThanOrEqualTo(new Date());
            criteria.andEndTimeGreaterThanOrEqualTo(new Date());
            criteria.andStockCountGreaterThan(0);
            seckillList = tbSeckillGoodsMapper.selectByExample(example);
            for (TbSeckillGoods goods : seckillList) {
                redisTemplate.opsForHash().put(key,goods.getId(),goods);
                redisTemplate.opsForValue().set(goods.getId(),goods.getStockCount().toString());
            }
        }
        return seckillList;
    }
}
